<template>
   <div class="app-container">
      <h2>WorkerTest</h2>
      <span>{{ count }}</span>
   </div>
</template>

<script setup name="WorkerTest">
const count = ref(0)
// 计算任务1
let calculate1 = new Promise((resolve, reject) => {
   const worker = new Worker(new URL('./cumsumWorker.js', import.meta.url), {type: 'module'})
   let length = 1000000000;
   worker.postMessage(length);
   worker.onmessage = e => {
      worker.terminate()
      resolve(e.data)
   }
})
// 计算任务2
let calculate2 = new Promise((resolve, reject) => {
   const worker = new Worker(new URL('./cumsumWorker.js', import.meta.url), {type: 'module'})
   let length = 2000000000;
   worker.postMessage(length);
   worker.onmessage = e => {
      worker.terminate()
      resolve(e.data)
   }
})
// 任务汇总
Promise.all([calculate1, calculate2]).then((res) => {
   console.log(res)
   count.value = res
})

</script>